package com.gtenacg.cartoonservice.controller;

import com.gtenacg.cartoonservice.common.annotation.CheckToken;
import com.gtenacg.cartoonservice.common.Result;
import com.gtenacg.cartoonservice.pojo.model.CartoonType;
import com.gtenacg.cartoonservice.service.CartoonTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cartoonType")
@RequiredArgsConstructor
public class CartoonTypeController {

    final CartoonTypeService cartoonTypeService;

    @GetMapping("/list")
    public Result<List<CartoonType>> list() {
        log.info("获取漫画类型");
        List<CartoonType> list = cartoonTypeService.list();
        return Result.success(list);
    }
    @GetMapping
    public Result<List<CartoonType>> search(
            @RequestParam(required = false) String name
    ) {
        log.info("获取漫画类型");
        List<CartoonType> list = cartoonTypeService.search(name);
        return Result.success(list);
    }

    @CheckToken
    @PostMapping()
    public Result<Integer> insert(@RequestBody CartoonType cartoonType) {
        log.info("插入漫画类型");
        Integer id = null;
        try {
            id = cartoonTypeService.insert(cartoonType);
        } catch (DuplicateKeyException e) {
            return new Result(HttpStatus.CONFLICT.value(), "Duplicate_Key", null);
        }
        return Result.success(id);
    }

    @CheckToken
    @PutMapping()
    public Result<Void> update(@RequestBody CartoonType cartoonType) {
        log.info("更新漫画类型");
        cartoonTypeService.update(cartoonType);
        return Result.success();
    }

    @CheckToken
    @DeleteMapping("/{ids}")
    public Result<Void> delete(@PathVariable List<Integer> ids) {
        log.info("删除漫画类型");
        cartoonTypeService.delete(ids);
        return Result.success();
    }
}
